package com.efei.jdbc.sql;

import org.springframework.util.StringUtils;

public class SqlInsert extends SqlCommon{

    private final StringBuilder cols = new StringBuilder();

    private final StringBuilder values = new StringBuilder();

    private String table;

    SqlInsert(){
        super(SqlType.INSERT);
    }


    public SqlInsert table(String table) {
        this.table = table;
        return this;
    }


    public SqlInsert insertCol(String col, String alis) {
        if (StringUtils.hasLength(col)) {
            return this;
        }

        if (this.cols.length() > 0) {
            this.values.append(",");
            this.cols.append(",");
        }

        cols.append(col);
        values.append(":").append(alis);
        return this;
    }

    public SqlInsert insertCol(String col) {
        return insertCol(col, col);
    }

    public SqlInsert insertCol(SqlColumn col) {
        if (this.cols.length() > 0) {
            this.values.append(",");
            this.cols.append(",");
        }

        cols.append(col.getName());
        values.append(":" + col.getAlis());
        addParameter(new SqlParameter(col.getAlis(), col.getValue(), col.getParmeterType()));
        return this;
    }

    @Override
    public StringBuilder getSql() {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ");
        sql.append(this.table);
        sql.append(" (");
        sql.append(cols);
        sql.append(") values ");
        sql.append("(");
        sql.append(this.values);
        sql.append(")");
        return sql;
    }
}
